package com.fengxing.ams.web.action;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fengxing.ams.pub.AMSException;
import com.fengxing.ams.pub.manager.intf.IConfigManager;
import com.fengxing.ams.web.action.view.LoginUserVO;
import com.fengxing.ams.web.action.view.ResponseVO;
import com.fengxing.ams.web.common.IContextManager;
import com.fengxing.ams.web.facade.intf.INotifyFacade;
import com.fengxing.ams.web.facade.intf.IOrderFacade;
import com.fengxing.ams.web.facade.intf.ISysManagerFacade;
import com.fengxing.ams.web.facade.intf.IUserFacade;
import com.fengxing.ams.web.facade.mouble.user.MenuVO;
import com.fengxing.ams.web.facade.mouble.user.UserVO;

@Controller
@RequestMapping("user")
@Transactional
public class UserAction {
	
	private Log logger = LogFactory.getLog(UserAction.class);
	
	@Autowired
	private IUserFacade userFacade;
	@Autowired
	private INotifyFacade notifyFacade;
	@Autowired
	private IContextManager contextManager;
	@Autowired
	private IConfigManager configManager;
	@Autowired
	private ISysManagerFacade sysManagerFacade;
	@Autowired
	private IOrderFacade orderFacade;
	
	private static final Random r = new Random();
	
	
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO loginUser(@RequestBody@Valid LoginUserVO loginUserVO, BindingResult result, HttpServletRequest req) {
		if (result.hasErrors()) {
			for (ObjectError erro :result.getAllErrors()) {
				String msg = erro.getDefaultMessage();
				throw new AMSException(msg);
			}
		}
		//先检查context中是否有user
		ResponseVO vo = new ResponseVO();
		UserVO loginedUser = this.contextManager.getUserVO();
		if (loginedUser == null) {
			UserVO userVO = new UserVO();
			userVO.setEmail(loginUserVO.getEmail());
			userVO.setPassword(loginUserVO.getPassword());
			//查询数据，看看邮箱地址是否有重复
			loginedUser = this.userFacade.login(userVO);
		}
		
		//生成token
		vo.setData(loginedUser);
		
		//如果有上次发送的code，且没有过期，则直接发送
		vo.setMessage("登录成功！");
		return vo;
	}
	
	@RequestMapping(value = "/logout.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO logoutUser(HttpServletRequest req) {
		//先检查context中是否有user
		ResponseVO vo = new ResponseVO();
		UserVO loginedUser = this.contextManager.getUserVO();
		this.contextManager.deleteSession(loginedUser);
		//如果有上次发送的code，且没有过期，则直接发送
		vo.setMessage("退出成功！");
		return vo;
	}
	
	@RequestMapping(value = "/checkToken.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO checkToken(HttpServletRequest req) {
		//先检查context中是否有user
		ResponseVO vo = new ResponseVO();
		vo.setSuccess(true);
		return vo;
	}
	
	@RequestMapping(value = "/menu.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO menuUser(HttpServletRequest req) {
		ResponseVO vo = new ResponseVO();
		
		UserVO userVO = this.contextManager.getUserVO();
		List<MenuVO> menus = this.userFacade.getUserMenu(userVO.getRoleIdList());
		
		Map<Long, MenuVO> menuMap = new LinkedHashMap<Long, MenuVO> ();
		for (MenuVO menu : menus) {
			if (menu.getParentId().longValue() == 0L) {
				menuMap.put(menu.getMenuId(), menu);
			}
		}
		
		for (MenuVO menu : menus) {
			if (menu.getParentId().longValue() != 0L) {
				MenuVO parentMenu = menuMap.get(menu.getParentId());
				parentMenu.getChild().add(menu);
				
			}
		}
		vo.setData(menuMap.values());
		
		//如果有上次发送的code，且没有过期，则直接发送
		vo.setMessage("获取菜单成功！");
		return vo;
	}
	
	
}
